package com.lujianfei.searchlistview;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lujianfei.searchlistview.widget.ListViewExt;
import com.lujianfei.searchlistview.widget.SearchBar;

import java.util.ArrayList;

import androidx.appcompat.app.AppCompatActivity;
import searchlistview.lujianfei.com.searchlistview.R;

public class MainActivity extends AppCompatActivity {

    ListViewExt listview;
    SearchBar mSearchBar;
    ArrayList<String> mList = new ArrayList<>();
    MyAdapter mAdappter;
    LinearLayout layout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        listview = findViewById(R.id.listview);

        mSearchBar = findViewById(R.id.search_bar);
        listview.setSearchlayout(mSearchBar);
        layout=(LinearLayout)LayoutInflater.from(this).inflate(R.layout.header_test,null);
        layout.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        mSearchBar.setTextEditable(false);
        layout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 15/11/23  the click event
                Log.e("MainActivity", "SearchBar click");

            }
        });
        listview.addHeaderView(new SearchBar(this));

        //initialize
        for (int i = 0; i < 50; i++) {
            String str = "item" + (i + 1);
            mList.add(str);
        }
        mAdappter = new MyAdapter();
        listview.setAdapter(mAdappter);

        //带刷新功能的调用实例
        testForPullToRefresh();

    }

    public void testForPullToRefresh(){
    }

    class MyAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return mList.size();
        }

        @Override
        public String getItem(int position) {
            return mList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View view = LayoutInflater.from(MainActivity.this).inflate(R.layout.item, null);
            TextView textView = (TextView) view.findViewById(R.id.textView);
            textView.setText(getItem(position));

            return view;
        }
    }
}
